import 'package:flutter/material.dart';

import '../../res/resources.dart';
import '../../utils/toast.dart';
import '../../utils/utils.dart';
import 'discover.dart';
import 'home.dart';
import 'mine.dart';
import 'sport.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  int _tabIndex = 0;
  var _pageList;
  var _tabImages;
  var _appBarTitles = ['首页', '运动', '发现', '我的'];
  final _pageController = PageController();

  @override
  void initState() {
    super.initState();
    initData();
  }

  void initData() {
    _pageList = [
      Home(),
      Sport(),
      Discover(),
      Mine(),
    ];

    _tabImages = [
      [
        Image.asset(Utils.getImgPath("demo/btn_home_noraml"), height: 25, width: 25,fit: BoxFit.fill,),
        Image.asset(Utils.getImgPath("demo/btn_home_select"), height: 25, width: 25,fit: BoxFit.fill,),
      ],
      [
        Image.asset(Utils.getImgPath("demo/ic_index_ex_normal"), height: 25, width: 25,fit: BoxFit.fill,),
        Image.asset(Utils.getImgPath("demo/ic_index_ex_selected"), height: 25, width: 25,fit: BoxFit.fill,),
      ],
      [
        Image.asset(Utils.getImgPath("demo/btn_discovery_normal"), height: 25, width: 25,fit: BoxFit.fill,),
        Image.asset(Utils.getImgPath("demo/btn_discovery_select"), height: 25, width: 25,fit: BoxFit.fill,),
      ],
      [
        Image.asset(Utils.getImgPath("demo/btn_user_normal"), height: 25, width: 25,fit: BoxFit.fill,),
        Image.asset(Utils.getImgPath("demo/btn_user_select"), height: 25, width: 25,  fit: BoxFit.fill,),
      ]
    ];
  }

  Image _getTabIcon(int curIndex) {
    if (curIndex == _tabIndex) {
      return _tabImages[curIndex][1];
    }
    return _tabImages[curIndex][0];
  }

  Widget _buildTabText(int curIndex) {
    return Padding(
      padding: const EdgeInsets.only(top: 2.0),
      child: Text(_appBarTitles[curIndex]),
    );
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _isExit,
      child: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            backgroundColor: Colors.white,
            items: <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                  icon: _getTabIcon(0), title: _buildTabText(0)),
              BottomNavigationBarItem(
                  icon: _getTabIcon(1), title: _buildTabText(1)),
              BottomNavigationBarItem(
                  icon: _getTabIcon(2), title: _buildTabText(2)),
              BottomNavigationBarItem(
                  icon: _getTabIcon(3), title: _buildTabText(3)),
            ],
            type: BottomNavigationBarType.fixed,
            currentIndex: _tabIndex,
            elevation: 5.0,
            iconSize: 25.0,
            selectedFontSize: Dimens.font_sp_12,
            unselectedFontSize: Dimens.font_sp_12,
            selectedItemColor: Colorful.text_main,
            unselectedItemColor: Color(0xffbfbfbf),
            onTap: (index) {
              _pageController.jumpToPage(index);
            },
          ),
          body: PageView(
            controller: _pageController,
            onPageChanged: _onPageChanged,
            children: _pageList,
            physics: NeverScrollableScrollPhysics(), // 禁止滑动
          )),
    );
  }

  void _onPageChanged(int index) {
    setState(() {
      _tabIndex = index;
    });
  }


  DateTime _lastTime;

  Future<bool> _isExit() {
    if (_lastTime == null ||
        DateTime.now().difference(_lastTime) > Duration(milliseconds: 2500)) {
      _lastTime = DateTime.now();
      Toast.show("再次点击退出应用");
      return Future.value(false);
    }
    Toast.cancelToast();
    return Future.value(true);
  }
}
